import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router';
import PageRouter from './page/'
import ViewsRouter from './views/'
import AvueRouter from './avue-router';
import i18n from '/src/lang/index.js'
import {useUserStore} from "../store/modules/user.js";


// Basic routing without permission
export const basicRoutes = [
  ...PageRouter, ...ViewsRouter,
];
// 白名单应该包含基本静态路由
const WHITE_NAME_LIST = [];
const getRouteNames = (array) =>
    array.forEach((item) => {
      WHITE_NAME_LIST.push(item.name);
      getRouteNames(item.children || []);
    });
getRouteNames(basicRoutes);


//创建路由
const Router = createRouter({
  base: import.meta.env.VITE_APP_BASE,
  history: createWebHistory(),
  routes: basicRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      if (from.meta.keepAlive) {
        from.meta.savedPosition = document.body.scrollTop;
      }
      return {
        left: 0,
        top: to.meta.savedPosition || 0
      }
    }
  },
})
// console.log("11")
// const aa = {...mapStores(useUserStore, useLogsStore, useHrmStore)}
// console.log("aa", aa)
// console.log("aa", aa.hrmStore)
// console.log("aa", aa.hrmStore())
// console.log("aa hrmUserInfo", aa.hrmStore.hrmUserInfo)
// console.log("aa hrmShowType", aa.hrmStore().hrmShowType)
// console.log("aa hrmUserList", aa.hrmStore().hrmUserList)
// console.log("aa hrmUserList2", aa.hrmStore().Log("aatest"))

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  Router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !WHITE_NAME_LIST.includes(name)) {
      Router.hasRoute(name) && Router.removeRoute(name);
    }
  });
}

// config router
export function setupRouter(app) {
  AvueRouter.install({
    router: Router,
    i18n: i18n
  });
  const userStore = useUserStore()
  Router.$avueRouter.formatRoutes(userStore.menuAll, true);
  app.use(Router);
}
export default Router

export const asyncRouterMap = [
  // { type: 'crm', router: crmRouter },
  // { type: 'taskExamine', router: taskExamineRouter },
  // { type: 'workLog', router: workLogRouter },
  // { type: 'addressBook', router: addressBookRouter },
  // { type: 'project', router: pmRouter },
  // { type: 'bi', router: biRouter },
  // { type: 'calendar', router: calendarRouter },
  // { type: 'hrm', router: hrmRouter },
  // { type: 'manage', router: adminRouter }
]
